Exit status

The exit status or return code of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred to as an errorlevel.

When software programs are run or executed, the operating system creates an abstract entity called a computer process in which all the book-keeping for that program is accomplished. In multitasking operating systems such as UNIX or Linux, new processes can be created by active processes. Such processes are parent processes while those created are child processes. It is common in programming for a parent process to delegate some work to a child process it creates while it may remain busy with something else. When the child has finished executing, it will exit by calling the exit system call. This system call facilitates passing a small number back to the parent, which can retrieve this value using the wait system call. This is the exit status of the child.

Contents

Semantics

The parent and the child can have an understanding about the meaning of the exit statuses. For example, it is common programming practice for a child process to return zero to the parent signifying success. Apart from this return value from the child, other information like how the process exited, either normally or by a signal may also be available to the parent process.

The specific set of codes returned is unique to the program that sets it. Typically it indicates success or failure. The value of the code returned by the function or program may indicate a specific cause of failure. On many systems, the higher the value, the more severe the cause of the error.[1] Alternatively, each bit may indicate a different condition, which are then ored together to give the final value; for example, fsck does this.

Sometimes, if the codes are designed with this purpose in mind, they can be used directly as a branch index upon return to the initiating program to avoid additional tests.

C

The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these evaluate to 0 and 1 respectively.[2] A C program may also use the exit() function specifying the integer status or exit macro as the first parameter. The status may be an integer between 0 and 255 and should follow any existing conventions for programs of its type, however typically 0 indicates success and 1 indicates failure.[3]

Java

In Java, any method can call System.exit(int status), unless a security manager does not permit it. This will terminate the currently running Java Virtual Machine. "The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination." [4] (int status is the errorlevel.)

Unix

In Unix, the wait system call sets a status value of type int packed as a bitfield with various types of child termination information. If the child terminated by exiting (as determined by the WIFEXITED macro; the usual alternative being that it died from an uncaught signal), SUS specifies that the low-order 8 bits of the status value contain the exit status;[5] this can be retrieved using the WEXITSTATUS macro in wait.h.[6] As such, on Unix exit statuses are restricted to values 0-255, the range of an unsigned 8-bit integer.

Unix-like systems typically use a convention of zero for success and non zero for error.[7] Some conventions have developed as to the relative meanings of various error codes; for example GNU recommend that codes with the high bit set be reserved for serious errors,[2] and FreeBSD have documented an extensive set of preferred interpretations.[8]

DOS

In DOS terminology, an errorlevel is an integer exit code returned by an executable program or subroutine. Errorlevels typically range from 0 to 255. In DOS there are only 256 error codes available.

OpenVMS

In OpenVMS success is indicated by odd values and failure by even values. The value is a 32 bit integer with sub-fields: control bits, facility number, message number and severity. Severity values are divided between success (Success, Informational) and failure (Warning, Error, Fatal).OpenVMS Format of Return Status Values

Windows

Windows uses 32-bit signed integers as exit codes.[9] If a process fails initialization, a Windows system error code may be returned.[10] Windows system error codes are available online.[11]

Exit codes are directly referenced, for example, by the command line interpreter CMD.exe in the errorlevel terminology inherited from DOS. .NET Framework processes and the Windows PowerShell refer to it as the ExitCode property of the Process object.

See also

References